// import GamePlayFunction from "../Tools/GamePlayFunction";
import ShuaiAPI from "../Tools/ShuaiAPI";
import GameManager from "./GameManager";
import InputManager from "./InputManager";
// 
const { ccclass, property } = cc._decorator;

@ccclass
export default class TouchManager extends cc.Component {
    public static Instance: TouchManager;

    startPos: cc.Vec3;
    currentPos: cc.Vec3;
    start_x: number;
    start_y: number;
    endPos: cc.Vec3;

    isPress: boolean = false;

    touchDistance = 70;

    onLoad() {
        TouchManager.Instance = this;
    }

    start() {
        this.registerEvent();
    }

    registerEvent() {
        this.node.on(cc.Node.EventType.TOUCH_START, this.onEventStart, this);
        this.node.on(cc.Node.EventType.TOUCH_MOVE, this.onEventMove, this);
        this.node.on(cc.Node.EventType.TOUCH_CANCEL, this.onEventCancel, this);
        this.node.on(cc.Node.EventType.TOUCH_END, this.onEventEnd, this);
    }

    //触摸开始
    onEventStart(event) {
        //世界坐标
        this.startPos = event.getLocation();
        // this.currentPos = event.getLocation();
        this.start_x = this.startPos.x;
        this.start_y = this.startPos.y;

        this.isPress = true;
        this.canMoveV = true;

        // console.log('start Event \n worldPoint=', this.startPos);
    }

    canMoveV = true;
    // 触摸移动
    onEventMove(event) {
        if (!GameManager.Instance.isCanOperate) {
            return;
        }

        // if (this.isPress == true) {
        this.currentPos = event.getLocation();
        // this.start_x = event.getLocation().x;
        // this.start_y = event.getLocation().y;
        let diff_x: number = this.currentPos.x - this.start_x;
        let diff_y: number = this.currentPos.y - this.start_y;

        let diff_max = Math.abs(diff_x) >= Math.abs(diff_y) ? diff_x : diff_y;
        if (Math.abs(diff_max) >= this.touchDistance) {
            // this.isPress = false;

            let _direction: cc.Vec2 = null;
            if (Math.abs(diff_x) >= Math.abs(diff_y)) {
                if (diff_x > 0) {
                    _direction = ShuaiAPI.Vec2.RIGHT;

                    this.start_x = this.currentPos.x;
                    this.start_y = this.currentPos.y;
                    InputManager.Instance.右()
                }
                else {
                    _direction = ShuaiAPI.Vec2.LEFT;

                    this.start_x = this.currentPos.x;
                    this.start_y = this.currentPos.y;
                    InputManager.Instance.左()
                }
            }
            else {
                if (this.canMoveV === true) {
                    if (diff_y > 0) {
                        _direction = ShuaiAPI.Vec2.UP;

                        InputManager.Instance.上()
                    }
                    else {
                        _direction = ShuaiAPI.Vec2.DOWN;

                        InputManager.Instance.下()
                    }

                    this.canMoveV = false
                }

            }

            // console.log('_direction', _direction);

            // GamePlayFunction.OperateSlide(_direction);
        }
        // }
    }

    /**
     * 触摸
     * 当手指在目标节点区域外离开屏幕时
     * 比如说，触摸node的size是200x200。
     * 当超过这个区域时，就是触发这个事件
     * @param {*} event
     */
    onEventCancel(event) {
        this.endPos = event.getLocation();

        this.isPress = false;
    }

    //当手指在目标节点区域内离开屏幕时
    onEventEnd(event) {
        this.endPos = event.getLocation();

        this.isPress = false;
    }

}
